﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace Global
{
    public class Coagulation_method
    {
        public static int GetSec(ObservableCollection<int> data ,int backNum)
        {
            int tempdata = data[backNum+1];

            for (int i = 0; i < backNum + 1; i++) //试剂添加抖动处理
            {
                data[i] = tempdata;
            }

            for (int i = 0; i < data.Count; i++) //试剂添加抖动处理
            {
                data[i] = tempdata - data[i];
            }

            data = GetMedienSmoothOptData(data, 2, 2);   //中值平滑点
            data = GetShiftAveSmoothOptData(data, 2, 2); //平均值平滑


            int Maxdata = data.Max();
            int Mindata = data.Min();
            int avageData = (Maxdata  - Mindata) / 2 + Mindata;
            //寻找最接近中值的数据序号，作为时间轴
            int resultIndex = 0;
            for(int i = 0; i < data.Count; i++)
            {
                if(data[i] > avageData) //找到第一个大于avageData的点
                {
                    resultIndex = i;
                    break;
                }
            }
            // = data.IndexOf((Maxdata - Mindata)/2);

            return resultIndex;
        }
        public static int GetOD(ObservableCollection<int> data, int backNum)
        {
            int tempdata = data[backNum + 1];
            for (int i = 0; i < backNum + 1; i++) //试剂添加抖动处理
            {
                data[i] = tempdata;
            }            

            //for (int i = 0; i < data.Count; i++) //试剂添加抖动处理
            //{
            //    data[i] = tempdata - data[i];
            //}
            //for (int i = 1; i < data.Count-1; i++) //跳变数据处理排序
            //{
            //    if(data[i]>data[i-1] && data[i]>data[i+1])
            //       data[i] = (data[i] + data[i + 1])/2;
            //    else if(data[i] < data[i - 1] && data[i] <data[i + 1])
            //       data[i] = (data[i] + data[i + 1]) / 2;

            data = GetMedienSmoothOptData(data, 2, 2);   //中值平滑点
            data = GetShiftAveSmoothOptData(data, 2, 2); //平均值平滑
            //}
            int Maxdata = data.Max();
            int Mindata = data.Min();
            int avageData = (Maxdata - Mindata) / 2;
            //寻找最接近中值的数据序号，作为时间轴
            int resultIndex = 0;
            for (int i = 0; i < data.Count; i++)
            {
                if (data[i] > avageData) //找到第一个大于avageData的点
                {
                    resultIndex = i;
                    break;
                }
            }
            // = data.IndexOf((Maxdata - Mindata)/2);

            return resultIndex;
        }
        public static int GetSecTest(ObservableCollection<int> data)
        {
            int tempdata = data[11];

            for (int i = 11; i < data.Count; i++) //试剂添加抖动处理
            {
                data[i] =  data[i] - tempdata ;
            }
            for (int i = 1; i < data.Count - 1; i++) //跳变数据处理排序
            {
                if (data[i] > data[i - 1] && data[i] > data[i + 1])
                    data[i] = (data[i] + data[i + 1]) / 2;
                else if (data[i] < data[i - 1] && data[i] < data[i + 1])
                    data[i] = (data[i] + data[i + 1]) / 2;


            }
            int Maxdata = data.Max();
            int Mindata = data.Min();
            int avageData = (Maxdata - Mindata) / 2;
            //寻找最接近中值的数据序号，作为时间轴
            int resultIndex = 0;
            for (int i = 0; i < data.Count; i++)
            {
                if (data[i] > avageData) //找到第一个大于avageData的点
                {
                    resultIndex = i;
                    break;
                }
            }
            // = data.IndexOf((Maxdata - Mindata)/2);

            return resultIndex;
        }


        public static ObservableCollection<int> GetMedienSmoothOptData(ObservableCollection<int> optData, int before_p, int after_p)
        {
            ObservableCollection<int> numArray = new ObservableCollection<int>();
            int index = 0;
            int num2 = (before_p + 1) + after_p;
            double[] array = new double[num2];
            double num3 = 0.0;
            for (int i = 0; i < optData.Count; i++)
            {
                if (((num2 > 1) && (i >= before_p)) && (i < (optData.Count - after_p)))
                {
                    index = 0;
                    for (int j = i - before_p; j <= (i + after_p); j++)
                    {
                        array[index] = optData[j];
                        index++;
                    }
                    Array.Sort<double>(array);
                    num3 = array[index / 2];
                }
                else
                {
                    num3 = optData[i];
                }
                numArray.Add((int)num3);
            }
            return numArray;
        }
        public static ObservableCollection<int> GetShiftAveSmoothOptData(ObservableCollection<int> optData, int before_p, int after_p)
        {
            ObservableCollection<int> numArray = new ObservableCollection<int>();
            int num = (before_p + 1) + after_p;
            int num2 = 0;
            for (int i = 0; i < optData.Count; i++)
            {
                if (((num > 1) && (i >= before_p)) && (i < (optData.Count - after_p)))
                {
                    num2 = 0;
                    for (int j = i - before_p; j <= (i + after_p); j++)
                    {
                        num2 += optData[j];
                    }
                    numArray.Add((int)(num2 / num));
                }
                else
                {
                    numArray.Add(optData[i]);
                }
            }
            return numArray;
        }

    }
}
